﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

namespace IronSuit.DataMap
{
	public class MSDBFactory : IDisposable,IDBFactory
	{
		SqlConnection Conn;
		public MSDBFactory()
		{
			string connStr = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
			Conn = new SqlConnection(connStr);
			Conn.Open();
		}
		public MSDBFactory(string connKey)
		{
			string connStr = ConfigurationManager.ConnectionStrings[connKey].ConnectionString;
			Conn = new SqlConnection(connStr);
			Conn.Open();
		}
		public MSDBFactory(DbConnection conn)
		{
			Conn = conn as SqlConnection;
			Conn.Open();
		}

		#region IDisposable Members

		public void Dispose()
		{
			if (Conn != null && Conn.State != ConnectionState.Closed)
				Conn.Close();
		}

		#endregion

		public void Close()
		{
			Dispose();
		}
		public int ExecSql(string Sql, List<SqlParameter> ParaList)
		{
			if (string.IsNullOrEmpty(Sql))
				return 0;
			SqlCommand Comm = new SqlCommand(Sql, Conn);
			if (ParaList != null)
				Comm.Parameters.AddRange(ParaList.ToArray());
			int affectCount = Comm.ExecuteNonQuery();
			return affectCount;
		}
		public int ExecSql(string Sql, List<DbParameter> ParaList)
		{
			return ExecSql(Sql, ParaList.ConvertAll<SqlParameter>(ParametrConverter));
		}
		public static SqlParameter ParametrConverter(DbParameter pf)
		{
			return pf as SqlParameter;
		}
		public DataSet ExecQuerySql(string Sql, List<DbParameter> ParaList)
		{
			return ExecQuerySql(Sql, ParaList.ConvertAll<SqlParameter>(ParametrConverter));
		}
		public DataSet ExecQuerySql(string Sql, List<SqlParameter> ParaList)
		{
			if (string.IsNullOrEmpty(Sql))
				return null;
			SqlCommand Comm = new SqlCommand(Sql, Conn);
			if (ParaList != null)
				Comm.Parameters.AddRange(ParaList.ToArray());
			SqlDataAdapter Adapter = new SqlDataAdapter(Comm);
			DataSet DS = new DataSet();
			Adapter.Fill(DS);
			return DS;
		}
		public bool Insert(DataEntity Entity)
		{
			List<SqlParameter> ParaList;
			string Sql = Entity.GetDataHandler().GetInsertSql(out ParaList);
			int count = ExecSql(Sql, ParaList);
			if (count == 1)
				return true;
			else
				return false;
		}
		public bool Update(DataEntity Entity)
		{
			List<SqlParameter> ParaList;
			string Sql = Entity.GetDataHandler().GetUpdateSql(out ParaList);
			int count = ExecSql(Sql, ParaList);
			if (count == 1)
				return true;
			else
				return false;
		}

		public bool Delete(DataEntity Entity)
		{
			List<SqlParameter> ParaList;
			string Sql = Entity.GetDataHandler().GetDeleteSql(out ParaList);
			int count = ExecSql(Sql, ParaList);
			if (count == 1)
				return true;
			else
				return false;
		}
		public bool Query(DataEntity Entity)
		{
			List<SqlParameter> ParaList;
			string Sql = Entity.GetDataHandler().GetQuerySql(out ParaList);
			DataSet DS = ExecQuerySql(Sql, ParaList);
			if (DS.Tables[0].Rows.Count == 1)
			{
				DataTable DT = DS.Tables[0];
				Entity.FieldValueDic.Clear();
				foreach (DataColumn Column in DT.Columns)
				{
					Entity.FieldValueDic.Add(Column.ColumnName, DT.Rows[0][Column]);
				}
				return true;
			}
			else
				return false;
		}

		#region IDBFactory Members
		public List<DataEntity> QueryList(DataEntity Entity, int pageSize)
		{
			List<DataEntity> entityList = new List<DataEntity>();
			DataTable DT = QueryDataTable(Entity,pageSize );
			foreach (DataRow dr in DT.Rows)
			{
				DataEntity de = new DataEntity();
				foreach (DataColumn Column in DT.Columns)
				{
					de.FieldValueDic.Add(Column.ColumnName, dr[Column]);
				}
				entityList.Add(de);
			}
			return entityList;
		}

		public DataTable QueryDataTable(DataEntity Entity, int pageSize)
		{
			List<SqlParameter> ParaList;
			string Sql = Entity.GetDataHandler().GetQueryListSql(out ParaList,pageSize );
			DataSet DS = ExecQuerySql(Sql, ParaList);
			return DS.Tables[0];
		}
		public long Count(DataEntity Entity)
		{
			List<SqlParameter> ParaList;
			string Sql = Entity.GetDataHandler().GetCountSql(out ParaList);
			DataSet DS = ExecQuerySql(Sql, ParaList);
			return (long)DS.Tables[0].Rows[0][0];
		}

		#endregion
	}
}
